package com.vichat.user.controller;

import com.vichat.common.cache.RedisHelper;
import com.vichat.common.vo.Grid;
import com.vichat.common.vo.Json;
import com.vichat.online.entity.OnlineUserDLog;
import com.vichat.online.entity.OnlineUserMLog;
import com.vichat.online.service.IOnlineService;
import com.vichat.user.entity.UserBasic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * Created by wangluyin on 2018/3/19.
 */

@Controller
@RequestMapping("/vcapi/data/")
public class VCDataController extends VCApiController {
    protected static final Logger logger = LoggerFactory.getLogger(VCDataController.class);
    @Autowired
    HttpServletRequest request;
    @Autowired
    HttpServletResponse response;


    @Autowired
    private IOnlineService onlineService;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); // true:允许输入空值，false:不能为空值
    }

    /**
     * 获取人员当天在线时长排行榜
     *
     * @return
     */
    @RequestMapping("/queryOnlineUserMLogDay")
    @ResponseBody
    public Json queryOnlineUserMLogDay() {
        Json json = new Json();
        try {
            UserBasic sessionUser = getSessionUser();
            long partyId = sessionUser.getPartyId();
            List<OnlineUserMLog> onlineUserMLogs = onlineService.getOnlineUserMLogDayReport(partyId);
            if (onlineUserMLogs != null) {
                for (OnlineUserMLog log : onlineUserMLogs) {
                    log.setUserName(RedisHelper.getUname(log.getUid()));
                }
            }
            json.setObj(onlineUserMLogs);
            json.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
            json.setCode(-9);
        }
        return json;
    }

    /**
     * 获取人员本周在线时长排行榜
     *
     * @return
     */
    @RequestMapping("/queryOnlineUserMLogWeek")
    @ResponseBody
    public Json queryOnlineUserMLogWeek() {
        Json json = new Json();
        try {
            UserBasic sessionUser = getSessionUser();
            long partyId = sessionUser.getPartyId();
            List<OnlineUserMLog> onlineUserMLogs = onlineService.getOnlineUserMLogWeekReport(partyId);
            if (onlineUserMLogs != null) {
                for (OnlineUserMLog log : onlineUserMLogs) {
                    log.setUserName(RedisHelper.getUname(log.getUid()));
                }
            }
            json.setObj(onlineUserMLogs);
            json.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
            json.setCode(-9);
        }
        return json;
    }

    /**
     * 获取人员本月在线时长排行榜
     *
     * @return
     */
    @RequestMapping("/queryOnlineUserMLogMonth")
    @ResponseBody
    public Json queryOnlineUserMLogMonth() {
        Json json = new Json();
        try {
            UserBasic sessionUser = getSessionUser();
            long partyId = sessionUser.getPartyId();
            List<OnlineUserMLog> onlineUserMLogs = onlineService.getOnlineUserMLogMonthReport(partyId);
            if (onlineUserMLogs != null) {
                for (OnlineUserMLog log : onlineUserMLogs) {
                    log.setUserName(RedisHelper.getUname(log.getUid()));
                }
            }
            json.setObj(onlineUserMLogs);
            json.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
            json.setCode(-9);
        }
        return json;
    }


    @ResponseBody
    @RequestMapping("/queryOnlineUserDetailByPage")
    public Grid queryOnlineUserDetailByPage(long onlineType, String time, long uid, int page, int rows) {
        Grid grid = new Grid();
        try {
            UserBasic sessionUser = getSessionUser();
            long partyId = sessionUser.getPartyId();
            List<OnlineUserMLog> onlineUserMLogs = onlineService.getUserOnlineDetail(onlineType, time, uid, partyId, page, rows);
            Long count = onlineService.getUserOnlineDetailCount(onlineType, time, uid, partyId);
            grid.setRows(onlineUserMLogs);
            grid.setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
            grid.setErrorMsg(e.getMessage());
            grid.setCode(-9);
        }
        return grid;
    }

    /**
     * 获取人员在线日时长明细
     *
     * @param uid
     * @return
     */
    @RequestMapping("/getOnlineUserDLog")
    @ResponseBody
    public Json getOnlineUserDLog(long uid, String time) {
        Json json = new Json();
        try {
            List<OnlineUserDLog> onlineUserDLogs = onlineService.getOnlineUserDLogByUid(uid, time);
            json.setObj(onlineUserDLogs);
            json.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
            json.setCode(-9);
        }
        return json;
    }

    /**
     * 获取人员在线月时长明细
     *
     * @param uid
     * @return
     */
    @RequestMapping("/getOnlineUserMLog")
    @ResponseBody
    public Json getOnlineUserMLog(long uid, String time) {
        Json json = new Json();
        try {
            List<OnlineUserMLog> onlineUserMLogs = onlineService.getOnlineUserMLogByUid(uid, time);
            json.setObj(onlineUserMLogs);
            json.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
            json.setCode(-9);
        }
        return json;
    }

}
